package de.dhbw.karlsruhe.picsim.util.impl;

import de.dhbw.karlsruhe.picsim.util.Bits;
import de.dhbw.karlsruhe.picsim.util.BitsMutable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 14, 2010
 * Time: 10:49:50 AM
 */
public final class BitsMutableImpl extends BitSetSupport implements BitsMutable {
    private final List<Boolean> ourBits;

    public BitsMutableImpl(final List<Boolean> bits) {
        if (bits.contains(null)) {
            throw new IllegalArgumentException();
        }
        ourBits = new ArrayList<Boolean>(bits);
    }

    public BitsMutableImpl(final Bits bits) {
        final int length = bits.getLength();
        ourBits = new ArrayList<Boolean>(length);
        for (int i = 0; i < length; i++) {
            ourBits.add(bits.isSet(i));
        }
    }

    @Override
    public int getLength() {
        return ourBits.size();
    }

    @Override
    public boolean isSet(final int bitNr) {
        return ourBits.get(bitNr);
    }

    @Override
    public void setBit(final int where, final boolean value) {
        ourBits.set(where, value);
    }

    List<Boolean> getOurBits() {
        return ourBits;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append(super.toString());
        sb.append("de.dhbw.karlsruhe.picsim.util.impl.BitsMutableImpl");
        sb.append("{ourBits=").append(ourBits);
        sb.append('}');
        return sb.toString();
    }
}

